<template>
  <div class="app-container">

    <el-form :model="form" ref="form" label-width="100px" v-loading="formLoading">
      <el-form-item label="班级名称：" required>
        <el-input v-model="form.name"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>







    </el-form>
      <div>添加老师</div>

      老师名字：<el-input v-model="teacherName" style="width: 20%;"></el-input>



    <el-button type="primary" @click="submitTeacher" style="margin-left: 20px;">提交</el-button>
  </div>
</template>

<script>
import { mapGetters, mapState, mapActions } from 'vuex'
import klassApi from '@/api/klass'

export default {
  data () {
    return {
      form: {
        id: null,
        name: '',
      },
      teacherName: '',
      formLoading: false
    }
  },
  created () {
    let id = this.$route.query.id
    let _this = this
    if (id && parseInt(id) !== 0) {
      _this.formLoading = true
      klassApi.select(id).then(re => {
        _this.form = re.response
        _this.formLoading = false
      })
    }
  },
  methods: {
    submitTeacher(){
      klassApi.setTeacher(this.teacherName,this.form.id).then(res=>{
        this.teacherName = '';
        alert("添加成功")
      })
    },
    submitForm () {
      let _this = this
      this.formLoading = true

      klassApi.edit(this.form).then(data => {
        if (data.code === 1) {
          _this.$message.success(data.message)
          _this.delCurrentView(_this).then(() => {
            _this.$router.push('/klass/klass/list')
          })
        } else {
          _this.$message.error(data.message)
          _this.formLoading = false
        }
      }).catch(e => {
        _this.formLoading = false
      })
    },

    ...mapActions('tagsView', { delCurrentView: 'delCurrentView' })
  },
  computed: {
    ...mapGetters('enumItem', [
      'enumFormat'
    ]),

  }
}
</script>
